package com.test.velocity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.fqh.utils.FileCreateUtil;
import org.fqh.utils.StringUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 

/**
* @author fqh 
* @version 创建时间：2017年11月30日 下午5:49:37
* 类说明
*/
public class TemplateGenerator{
	
	private final static Logger logger=LoggerFactory.getLogger(TemplateGenerator.class);
	 
	 @Test
	 public void test() {
		   String tableName = "sys_user";
		   Map  tableStructs = GetTableStructs.getTableStruct(tableName);
		   Map<String,String> templatePath = new HashMap<>();
		   templatePath.put("template/entityTemplate.vm", "\\src\\main\\java\\com\\fqh\\manager\\entity\\MODEL.java");//生成model 
		 //  templatePath.put("template/daoTemplate.vm", "\\src\\main\\java\\com\\fqh\\manager\\dao\\MODELDao.java");//生成Dao层
		 //  templatePath.put("template/xmlTemplate.vm", "\\src\\main\\java\\com\\fqh\\manager\\dao\\MODELMapper.java");//生成mpper  
		 //  templatePath.put("template/serviceTemplate.vm", "\\src\\main\\java\\com\\fqh\\manager\\service\\MODELService.java");//生成service
		 //  templatePath.put("template/serviceImplTemplate.vm", "\\src\\main\\java\\com\\fqh\\manager\\service\\MODELServiceImpl.java");//生成service
		 //  templatePath.put("template/actionTemplate.vm", "\\src\\main\\java\\com\\fqh\\manager\\service\\MODELAction.java");//生成action
		   Generator(tableName,tableStructs, templatePath);
	 }
	  /**
	   * 生成代码 
	   * @param tableName  表名 
	   * @param tableStructs 表结构 
	   * @param templatePath   map<模版路径,生成文件路径+名称>
	   */
	   public static  void Generator(String tableName,Map<String,String> tableStructs,Map<String,String> templatePath) {
			 VelocityEngine ve = new VelocityEngine();
			 ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
			 ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
			  
			 ve.init();
			 
			 VelocityContext ctx = new VelocityContext();
             String modelName = StringUtils.getStringTableColumn(tableName,true);
             ctx.put("modelName", modelName);
			 ctx.put("tableName", tableName);
			 ctx.put("tableStructs", tableStructs); 
			 ctx.put("Time", new Date());
			 String rootPath =System.getProperty("user.dir") ; 
			Set<Entry<String, String>> entrySet = templatePath.entrySet();
			
			for (Entry<String, String> entry : entrySet) {
				Template t = ve.getTemplate(entry.getKey());
				String path = entry.getValue(); 
			    path = path.replaceAll("MODEL",modelName);
			    logger.info("生成文件路径:"+rootPath + path); 

		        File file = new File(rootPath + path);
		        FileCreateUtil.judeFileExists(file);
		        // File dir = new File(rootPath + path);
		        FileCreateUtil.judeDirExists(new File(file.getParent()));   
			    merge(t,ctx,rootPath +path);
		        logger.info(path+"文件生成成功...");
					 
			}
			 
		 }
		 private static void merge(Template template, VelocityContext ctx, String path) {
			 PrintWriter writer = null; 
			 try {
				 writer = new PrintWriter(path);
				 template.merge(ctx, writer);
				 writer.flush();
			 } catch (FileNotFoundException e) {
				 e.printStackTrace();
			 } finally {
				 writer.close();
			 }
		 }
	}